package com.tsd.system.service.impl;

import com.tsd.core.cache.CacheOperatorApi;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysDataAuthGroupMapper;
import com.tsd.system.dao.SysDataAuthMapper;
import com.tsd.system.entity.SysDataAuth;
import com.tsd.system.entity.SysDataAuthExt;
import com.tsd.system.entity.SysDataAuthGroupExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysDataAuthGroupService;
import com.tsd.system.service.SysDataAuthService;
import com.tsd.core.thread.ThreadPoolTaskUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysDataAuth 服务实现层
 *
 * @author admin
 * @date 2022/11/15 15:12:44
 */
@Service
public class SysDataAuthServiceImpl extends BaseServiceImpl implements SysDataAuthService {

    @Resource
    private SysDataAuthMapper sysDataAuthMapper;
    @Resource
    private SysDataAuthGroupMapper sysDataAuthGroupMapper;
    @Resource
    private SysDataAuthGroupService sysDataAuthGroupService;
    @Resource
    private CacheOperatorApi<List<SysDataAuthExt>> sysDataAuthCacheOperatorApi;

    @Override
    public SysDataAuth getSysDataAuthById(Long id) throws Exception {
        return sysDataAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysDataAuthExt queryBySid(String sid) throws Exception {
        return sysDataAuthMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysDataAuths(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            SysDataAuth old = sysDataAuthMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            sysDataAuthMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveSysDataAuth(SysDataAuthExt record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            sysDataAuthMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            sysDataAuthMapper.updateByPrimaryKeySelective(record);
        }
        sysDataAuthGroupService.saveSysDataAuthGroup(record, opUser);
    }

    @Override
    public List<SysDataAuthExt> findSysDataAuths(Map<String, Object> params, PageBean page) throws Exception {
        return sysDataAuthMapper.selectByPage(params, page);
    }

    @Override
    public List<SysDataAuthExt> findSysDataAuths(Map<String, Object> params) throws Exception {
        return sysDataAuthMapper.selectByParams(params);
    }

    @Override
    public List<SysDataAuthExt> findSysDataAuths4User(Map<String, Object> params) throws Exception {
        List<SysDataAuthExt> list = sysDataAuthMapper.select4UserByParams(params);
        if (!HlpUtils.isEmptyList(list)) {
            List<String> authList = ListUtil.map(list, SysDataAuthExt::getSid);
            List<SysDataAuthGroupExt> groupExtList = ThreadPoolTaskUtil.queryInList4Split(authList, itemList -> {
                SysDataAuthGroupExt groupExt = new SysDataAuthGroupExt();
                groupExt.setDr(0);
                groupExt.setAuth_list(itemList);
                return sysDataAuthGroupMapper.selectByEntity(groupExt);
            });
            if (!HlpUtils.isEmptyList(groupExtList)) {
                for (SysDataAuthExt authExt : list) {
                    List<SysDataAuthGroupExt> filterList = ListUtil.filter(groupExtList, t -> t.getAuth_sid().equals(authExt.getSid()));
                    authExt.setGroup_list(filterList);
                }
            }
        }
        return list;
    }

    @Override
    public void bathSaveSysDataAuth(List<SysDataAuthExt> list, SysUser opUser) throws Exception {
        if (HlpUtils.isEmptyList(list)) {
            return;
        }
        for (SysDataAuthExt authExt : list) {
            this.saveSysDataAuth(authExt, opUser);
        }
        List<String> userList = ListUtil.filterMap(list, t -> !HlpUtils.isEmpty(t.getUser_sid()), SysDataAuthExt::getUser_sid);
        if (!HlpUtils.isEmptyList(userList)) {
            sysDataAuthCacheOperatorApi.remove(userList);
            for (String userSid : userList) {
                sysDataAuthCacheOperatorApi.remove(Arrays.asList(userSid + "_row", userSid + "_field"));
            }
        }
        List<String> roleList = ListUtil.filterMap(list, t -> !HlpUtils.isEmpty(t.getRole_sid()), SysDataAuthExt::getRole_sid);
        if (!HlpUtils.isEmptyList(roleList)) {
            sysDataAuthCacheOperatorApi.remove(roleList);
        }
    }
}